#include "cuda-play/utils/benchmark.h"

#include <sstream>

#include "cuda-play/utils/time_cost.h"

namespace cuda_play {
namespace utils {
Task::Task(std::function<void()> &&func) : inner_func_(func) {}

void Benchmark::add_task(Task &&task, std::string task_name) {
  if (task_name.empty()) {
    task_name = "task#" + std::to_string(tasks_.size());
  }
  tasks_.emplace_back(task_name, task);
}

void Benchmark::run() {
  output_.clear();
  std::ostringstream os(output_);
  os << "running " << tasks_.size() << " tasks.";
  os << "=====================================\n";
  for (auto &t : tasks_) {
    TimeCost tc;
    os << "running task: " << t.first << "\n";
    t.second();
    uint64_t time_elapsed = tc.get_elapsed();
    os << "----------------time used " << time_elapsed
       << "us ------------------------\n";
  }
}

}  // namespace utils
}  // namespace cuda_play